<?php

namespace addons\csmitsm\library\xcore\xcore\utils;

class XcDashboardUtils
{
       /**
     * 根据纬度，将list的值转换为chart的data
     * adjustLineData(["2024-12-13","2024-12-14","2024-12-15"],$list,"createdate","cnt")
     * 返回： [10,20,30] 值是从list的createdate的记录sum(cnt)得出
     * @return array
     */
    public static function adjustLineData($weidus, $list, $key_fieldname, $count_fieldname)
    {
        $weidu_data = [];
        foreach ($weidus as $key => $weidu) {
            $weidu_data[$key] = 0;
        }

        foreach ($list as $item) {
            foreach ($weidus as $key => $weidu) {
                if ($weidu == $item[$key_fieldname]) {
                    $count = $item[$count_fieldname];
                    $weidu_data[$key] = $weidu_data[$key] + $count;
                }
            }
        }
        return $weidu_data;
    }


    /**
     * 隐藏经度为0的维度
        [$dimcodes, $facts] = XcDashboardUtils::hiddenZeroLabel(['W-1', 'W-2', 'W-3'], [[1, 0, 2],[3, 0, 4]]);
        return: $dimcodes=['W-1', 'W-3']; $facts=[[1, 2],[3, 4]];
     * @return array
     */
    public static function hiddenZeroLabel($dimcodes, $facts)
    {
        // 用于存储新的维度数组和经度值数组
        $newDimcodes = [];
        $newFacts = [];

        // 遍历维度数组和经度值数组
        for ($i = 0; $i < count($dimcodes); $i++) {
            // 检查当前维度的所有经度值是否都为0
            $allZero = true;
            foreach ($facts as $fact) {
                if ($fact[$i] != 0) {
                    $allZero = false;
                    break;
                }
            }
            // 如果不是全部为0，则保留该维度和对应的经度值
            if (!$allZero) {
                $newDimcodes[] = $dimcodes[$i];
                foreach ($facts as $key => $fact) {
                    $newFacts[$key][] = $fact[$i];
                }
            }
        }
        return [$newDimcodes, $newFacts];
    }
}
